<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Service\RoleService;
use App\Http\Requests\RoleRequest;
use App\Models\Role;

class RoleController extends CommentController
{
	private $_role;

	public function __construct()
	{
		$this->_role = RoleService::getInstance();
	}

	public function index(Request $request)
	{
        $model = Role::getInstance();
        $role = $model->paginate(15);
        $count = $model->countRole();
		return view('admin.role.index',['role'=>$role,'count'=>$count]);
	}

	/**
     * 添加页面
     * @author 朱旭
     */
	public function create()
    {
        $result = $this->_role->getRoleNode();
        $node = '';
        if ($result['code'] == 200) {
            $node = $result['data'];
        }
        return view('admin.role.create',['node'=>$node]);
    }

    /**
     * 添加方法
     * @author 朱旭
     */
    public function store(RoleRequest $request)
    {
        return $this->returnJson($this->_role->addToRole($request));
    }

     /**
     * 显示指定文章
     *
     * @param int $id
     * @return Response
     */
    public function show($id)
    {
        
    }

    /**
     * 显示编辑指定文章的表单页面
     *
     * @param int $id
     * @return Response
     */
    public function edit($id)
    {
        $result = $this->_role->getRoleNode();
        $res = $this->_role->getRoleAndRoleNode($id);
        $node = '';
        if ($result['code'] == 200) {
            $node = $result['data'];
        }
    	return view('admin.role.edit',['node'=>$node,'res'=>$res]);
    }

    /**
     * 在存储器中更新指定文章
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(RoleRequest $request, $id)
    {
        return $this->returnJson($this->_role->editRole($request, $id));
    }

    /**
     * 从存储器中移除指定文章
     *
     * @param int $id
     * @return Response
     */
    public function destroy($id)
    {
        return $this->returnArr($this->_role->delete($id));
    }

    /**
     * 更改状态
     * @author 朱旭
     */
    public function status($id)
    {
        return $this->returnArr($this->_role->status($id));
    }
}